<template>
    <div>
        <div class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
           <!--图片分类-->
            <div class="mui-scroll">
                <a :class="['mui-control-item',item.id == 0 ? 'mui-active':'']" v-for="item in typeList" :key="item.id" @click.prevent="getPhotosByType(item.id)">
                    {{ item.title }}
                </a>

            </div>

            <!--图片列表-->
            <ul class="photo-list">
                <router-link v-for="item in photosList" :key="item.id" tag="li" :to="'/home/photo/' + item.id">
                    <img v-lazy="item.img_url">

                    <div class="info">
                        <h1 class="info-title">{{ item.title }}</h1>
                        <div class="info-body">{{ item.zhaiyao }}</div>
                    </div>
                </router-link>
            </ul>
        </div>
    </div>
</template>

<script>
    import mui from "../../../lib/mui/js/mui.min.js"
    import {Toast} from "mint-ui"


    export default {
        data(){
            return {
                typeList: [],
                photosList: []
            }
        },
        mounted() {//在页面挂载好元素后加入滑动
            mui(".mui-scroll-wrapper").scroll({
                deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
            });
        },
        created(){
            this.getAllPhotoTypes();
            //默认选中全部，加载所有图片
            this.getPhotosByType(0);
        },
        methods:{
            //加载所有的图片分类
            getAllPhotoTypes(){
                //api/getimgcategory
                this.$http.get("api/getimgcategory").then(result => {
                    result = result.body;
                    if(result.status === 0){
                        this.typeList = result.message;
                        //由于接口中没有返回 全部，需手动添加
                        var all = {id:0,title:"全部"};
                        this.typeList.unshift(all);
                    }else{
                        Toast("获取图片分类出错");
                    }
                })
            },
            //加载所有图片
            getPhotosByType(typeId){
                this.$http.get("api/getimages/" + typeId).then(result => {
                    result = result.body;
                    if(result.status === 0){
                        this.photosList = result.message;
                    }else{
                        Toast("获取图片出错");
                    }
                })
            }
        }
    }
</script>

<style scoped>
    image[lazy=loading] {
        width: 40px;
        height: 300px;
        margin: auto;
    }

    img {
        width: 100%;
        vertical-align: middle;
    }

    li {
        background-color: #ccc;
        text-align: center;
        margin-bottom: 10px;
        box-shadow: 0 0 9px #999;
        position: relative;
    }

    .info {
        color: white;
        text-align: left;
        position: absolute;
        bottom: 0;
        background-color: rgba(0, 0, 0, 0.4);
        max-height: 84px;
    }

    .info-title {
        font-size: 14px;
    }
    .info-body {
        font-size: 13px;
    }


    .photo-list {
        list-style: none;
        margin-top: 25px;
        padding: 20px;
        padding-bottom: 0;
    }
</style>